<?php
namespace app\api\controller;
use think\Db;
use think\request;
use think\Controller;
use app\common\model\CorePayLog;
use app\common\controller\PaylogBaseController;
use app\common\controller\ApiBaseController;
class Payment extends Controller {
	protected function _initialize() {
		parent::_initialize();
	}
	 	//支付宝扫码支付
 public function qrcode_pay($sell_id, $token, $sn, $price, $subject) {
	/**
	 * 获取支付宝支付二维码支付
	 */
	require_once '/extend/dpay/f2fpay/model/builder/AlipayTradePrecreateContentBuilder.php';
	require_once '/extend/dpay/f2fpay/service/AlipayTradeService.php';
	//		import('dpay.f2fpay.model.builder.AlipayTradePrecreateContentBuilder');
	//		import('dpay.f2fpay.service.AlipayTradeService');
	$qrPayRequestBuilder = new \AlipayTradePrecreateContentBuilder();
	$qrPay = new \AlipayTradeService($config);
	$providerId = "2088021044805134";
	//系统商pid,作为系统商返佣数据提取的依据
	$extendParams = new \ExtendParams();
	$extendParams -> setSysServiceProviderId($providerId);
	$extendParamsArr = $extendParams -> getExtendParams();

	// 支付超时，线下扫码交易定义为5分钟
	$timeExpress = "5m";
	// 创建请求builder，设置请求参数
	//第三方应用授权令牌,商户授权系统商开发模式下使用
	$sell_id = $sell_id;
	//"2088102242613573";
	$appAuthToken = $token;
	//"201709BB522f469ee5a9406091fad8f66ccf3X57";//根据真实值填写
	$qrPayRequestBuilder -> setSellerId($sell_id);
	$qrPayRequestBuilder -> setAppAuthToken($appAuthToken);
	$qrPayRequestBuilder -> setOutTradeNo($sn);
	$qrPayRequestBuilder -> setTotalAmount(0.01);
	$qrPayRequestBuilder -> setTimeExpress($timeExpress);
	$qrPayRequestBuilder -> setSubject($subject?$subject:'消费');
	$qrPayRequestBuilder -> setBody("消费");
	//$qrPayRequestBuilder->setUndiscountableAmount($undiscountableAmount);
	//$qrPayRequestBuilder->setExtendParams($extendParamsArr);
	// 调用qrPay方法获取当面付应答
	$qrPayResult = $qrPay -> qrPay($qrPayRequestBuilder);
	//	根据状态值进行业务处理
	switch ($qrPayResult->getTradeStatus()) {
		case "SUCCESS" :
			$response = $qrPayResult -> getResponse();
			$url=$response -> qr_code;
			Header("Location: $url");
					exit;
			break;
		case "FAILED" :
			break;
		case "UNKNOWN" :
			if ($qrPayResult -> getResponse()) {
				print_r($qrPayResult -> getResponse());
			}
			break;
		default :
			echo "不支持的返回状态，创建订单二维码返回异常!!!";
			break;
	}
}
	//网商采宝回调处理
	public function callback() {
		$status=input("orderStatus");
		if($status=="PAY_SUC"){
			$sn=input("appOrderNo");
			file_put_contents("log.txt", $sn."----".now_time().PHP_EOL, FILE_APPEND);
			$pay_info = db('log_paylog')->where('order_sn',$sn)->find();
			if($pay_info['state'] ==1){
				$paylog = new PaylogBaseController();
				$paylog->print_log($sn,input('receiveAmount'));
			}else{
				echo '请勿重复付款';
			}
		}
		return "success";
	}
	//支付宝扫码回调
	public function ali_callback(){
		file_put_contents("ccc.txt", input());
	}
}
